﻿$(function () {
	var main = window.main;
	main.init();
});
(function ($, win) {
	var Main = function () {
		
	}

	Main.prototype = {
		//初始化
		init: function () {
			this.openPwd();
			this.queryProduct();
			this.initEvent();
		},
		queryProduct: function () {
			var that = this;
			$.ajax({
				url: 'queryMainMenu',
				type: 'post',
				dataType: 'json',
				data: {
					randomid: Math.random()
				},
				success: function (response) {
					that.initMenu();
					that.add_product(response);
				}
			});
		},
		initEvent: function () {
			var that = this;
			$('#menu_p').accordion({
				onSelect: function (title, index) {
					var menu_id = $('#menu' + index).val();
					$("ul[name='" + title + "']").tree({
						url: 'queryMenu',
						queryParams: {
							product_no: menu_id
						},
						animate: true,
						lines: true,//显示虚线效果    
						onClick: function (node) {// 在用户点击一个子节点即二级菜单时触发addTab()方法,用于添加tabs  
							that.addTab(node.text, node.url, null);
						}
					});
				}
			});
			$('#loginOut').click(function () {
				$.messager.confirm('系统提示', '您确定要退出本次登录吗?', function (r) {
					if (r) {
						location.href = 'logout';
					}
				});
			});
			$('#editpass').click(function () {
				$('#w').window('open');
			});

			$('#btnEp').click(function () {
				that.serverLogin();
			})

			$('#btnCancel').click(function () {
				that.closePwd();
			})
		},
		initMenu: function () {
			// 导航菜单绑定初始化
			$("#menu_p").accordion({
				animate: false
			});

			this.tabCloseEven();
		},
		add_product: function (data) {
			$.each(data, function (i, sm) {
				$('#menu_p').accordion('add', {
					title: sm.menu_name,
					selected: false,
					content: "<input type='hidden' id='menu" + i + "' value='" + sm.id + "' /><div style='padding-top:10px'><ul name='" + sm.menu_name + "'></ul></div>"
				});
			});
		},
		createFrame: function (url) {
			var s = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:99%;"></iframe>';
			return s;
		},
		addTab: function (subtitle, url, icon) {
			if (!$('#tabs').tabs('exists', subtitle)) {
				$('#tabs').tabs('add', {
					title: subtitle,
					content: this.createFrame(url),
					closable: true,
					icon: icon
				});
			} else {
				$('#tabs').tabs('select', subtitle);
				$('#mm-tabupdate').click();
			}
			this.tabClose();
		},
		tabClose: function () {
			/* 双击关闭TAB选项卡 */
			$(".tabs-inner").dblclick(function () {
				var subtitle = $(this).children(".tabs-closable").text();
				$('#tabs').tabs('close', subtitle);
			});
			/* 为选项卡绑定右键 */
			$(".tabs-inner").bind('contextmenu', function (e) {
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});

				var subtitle = $(this).children(".tabs-closable").text();

				$('#mm').data("currtab", subtitle);
				$('#tabs').tabs('select', subtitle);
				return false;
			});
		},
		// 绑定右键菜单事件
		tabCloseEven: function () {
			var that = this;
			// 刷新
			$('#mm-tabupdate').click(function () {
				var currTab = $('#tabs').tabs('getSelected');
				var url = $(currTab.panel('options').content).attr('src');
				$('#tabs').tabs('update', {
					tab: currTab,
					options: {
						content: that.createFrame(url)
					}
				});
			});
			// 关闭当前
			$('#mm-tabclose').click(function () {
				var currtab_title = $('#mm').data("currtab");
				$('#tabs').tabs('close', currtab_title);
			});
			// 全部关闭
			$('#mm-tabcloseall').click(function () {
				$('.tabs-inner span').each(function (i, n) {
					var t = $(n).text();
					$('#tabs').tabs('close', t);
				});
			});
			// 关闭除当前之外的TAB
			$('#mm-tabcloseother').click(function () {
				$('#mm-tabcloseright').click();
				$('#mm-tabcloseleft').click();
			});
			// 关闭当前右侧的TAB
			$('#mm-tabcloseright').click(function () {
				var nextall = $('.tabs-selected').nextAll();
				if (nextall.length == 0) {
					// msgShow('系统提示','后边没有啦~~','error');
					alert('后边没有啦~~');
					return false;
				}
				nextall.each(function (i, n) {
					var t = $('a:eq(0) span', $(n)).text();
					$('#tabs').tabs('close', t);
				});
				return false;
			});
			// 关闭当前左侧的TAB
			$('#mm-tabcloseleft').click(function () {
				var prevall = $('.tabs-selected').prevAll();
				if (prevall.length == 0) {
					alert('到头了，前边没有啦~~');
					return false;
				}
				prevall.each(function (i, n) {
					var t = $('a:eq(0) span', $(n)).text();
					$('#tabs').tabs('close', t);
				});
				return false;
			});

			// 退出
			$("#mm-exit").click(function () {
				$('#mm').menu('hide');
			});
		},
		// 弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
		msgShow: function (title, msgString, msgType) {
			$.messager.alert(title, msgString, msgType);
		},
		//关闭登录窗口
		closePwd: function () {
			$('#w').window('close');
		},
		//修改密码
		serverLogin: function () {
			var that = this;
			var $newpass = $('#txtNewPass');
			var $rePass = $('#txtRePass');

			if ($newpass.val() == '') {
				that.msgShow('系统提示', '请输入密码！', 'warning');
				return false;
			}
			if ($rePass.val() == '') {
				that.msgShow('系统提示', '请在一次输入密码！', 'warning');
				return false;
			}

			if ($newpass.val() != $rePass.val()) {
				that.msgShow('系统提示', '两次密码不一至！请重新输入', 'warning');
				return false;
			}

			$.post('/ajax/editpassword.ashx?newpass=' + $newpass.val(), function (msg) {
				that.msgShow('系统提示', '恭喜，密码修改成功！<br>您的新密码为：' + msg, 'info');
				$newpass.val('');
				$rePass.val('');
				that.close();
			})
		},
		//初始化修改密码窗口
		openPwd: function () {
			$('#w').window({
				title: '修改密码',
				width: 300,
				modal: true,
				shadow: true,
				closed: true,
				height: 160,
				resizable: false
			});
		}
	}

	win.main = win.main ||  new Main();
}(jQuery, window));